<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Account extends CI_Controller {
	
	private $tab ='account';
	
	function __construct(){
		parent::__construct();
		/** 登陆验证 */
		if(!isset($_SESSION)){
		    session_start();
		}
		$admin = $this->session->userdata('admin_user');
		if(!$admin || !isset($admin['user_id'])){
			header('Location: /ychadmin/login');
			exit;
		}
	}
	
	function accountlist(){
		$per_page 	= $this->input->get('per_page')?$this->input->get('per_page'):0;
		$username 	= $this->input->get('username');
		$realname 	= $this->input->get('realname');
		$endtime 	= $this->input->get('endtime');
		$ze_rank	= $this->input->get('ze_rank');
		$ky_rank	= $this->input->get('ky_rank');
		$dj_rank	= $this->input->get('dj_rank');
		$ds_rank	= $this->input->get('ds_rank');
		$is_loan 	= isset($_GET['is_loan']) ? $this->input->get('is_loan') : -1;
		$is_self 	= isset($_GET['is_self']) ? $this->input->get('is_self') : -1;
		
		$data = array();
		$orderby = '';
		$orderArr = array('desc','asc');
		$idNum = 0;
		$orderNum = 0;
		if($username != ''){
			$data['where']['username'] = $username;
		}
		if($realname != ''){
			$data['where']['realname'] = $realname;
		}
		if($endtime != ''){
			$data['where']['endtime'] = $endtime;
		}

		if($ze_rank && in_array($ze_rank, $orderArr)) {
			$orderby = ' total '.$ze_rank;
			$idNum = 1;
			$orderNum = $ze_rank == 'desc' ? 1 : 2;
		} else if ($ky_rank && in_array($ky_rank, $orderArr)) {
			$orderby = ' use_money '.$ky_rank;
			$idNum = 2;
			$orderNum = $ky_rank == 'desc' ? 1 : 2;
		} else if ($dj_rank && in_array($dj_rank, $orderArr)) {
			$orderby = ' no_use_money '.$dj_rank;
			$idNum = 3;
			$orderNum = $dj_rank == 'desc' ? 1 : 2;
		} else if ($ds_rank && in_array($ds_rank, $orderArr)) {
			$orderby = ' collection '.$ds_rank;
			$idNum = 4;
			$orderNum = $ds_rank == 'desc' ? 1 : 2;
		}
		$data['orderby'] = $orderby;
		if($is_loan != '' && $is_loan != -1){
			$data['where']['is_loan'] = $is_loan;
		}
		if($is_self != '' && $is_self != -1){
			$data['where']['is_self'] = $is_self;
		}
		
		$data['page_size']  = YTZ_LIST_PAGE_SIZE;
		$data['page_start'] = (int)$per_page;
		$this->load->model('Account_model');
		$vdata = $this->Account_model->get_account_user_list($data);
		
		//分页
		$this->load->library('pagination');
		$config['base_url'] = '/ychadmin/account/accountlist/?username='.$username.'&is_loan='.$is_loan.'&is_self='.$is_self.'&realname='.$realname.'&endtime='.$endtime;
		$config['total_rows'] = $vdata['account_count'];
		$config['per_page'] = YTZ_LIST_PAGE_SIZE; 
		$this->pagination->initialize($config); 
		
		$this->load->model('Accountcash_model');
		$vdata['account_bank'] = $this->Accountcash_model->arrays("account_bank");
		$this->load->model('Area_model', 'area');
		$vdata['user_loan'] = $this->Account_model->arrays("user_loan");
		$vdata['user_self'] = $this->Account_model->arrays("user_self");
		$vdata['area_list'] = $this->area->get_list();
		$vdata['username'] 	= $username;
		$vdata['realname'] 	= $realname;
		$vdata['endtime'] 	= $endtime;
		$vdata['is_loan'] 	= $is_loan;
		$vdata['is_self'] 	= $is_self;
		
		$vdata['tab'] = $this->tab;
		$vdata['sub'] = 'account';
		$vdata['id_num'] = $idNum;
		$vdata['order_num'] = $orderNum;
		$this->load->view('/ychadmin/account_list', $vdata);
	}
	
	function accountexcel(){
		$per_page 	= $this->input->get('per_page')?$this->input->get('per_page'):0;
		$username 	= $this->input->get('username');
		$realname 	= $this->input->get('realname');
		$endtime 	= $this->input->get('endtime');
		$is_loan 	= isset($_GET['is_loan']) ? $this->input->get('is_loan') : -1;
		$is_self 	= isset($_GET['is_self']) ? $this->input->get('is_self') : -1;
		$data = array();
		if($username != ''){
			$data['where']['username'] = $username;
		}
		if($realname != ''){
			$data['where']['realname'] = $realname;
		}
		if($endtime != ''){
			$data['where']['endtime'] = $endtime;
		}
		if($is_loan != '' && $is_loan != -1){
			$data['where']['is_loan'] = $is_loan;
		}
		if($is_self != '' && $is_self != -1){
			$data['where']['is_self'] = $is_self;
		}
		$data['limit'] = 'all';
		$data['page_size']  = YTZ_LIST_PAGE_SIZE;
		$data['page_start'] = 0;
		$this->load->model('Account_model');
		$this->load->model('Accountcash_model');
		$vdata = $this->Account_model->get_account_user_list($data);
		$this->load->model('Area_model', 'area');
		$area_list = $this->area->get_list();
		
		$this->load->library('PHPExcel');
		$this->load->library('PHPExcel/IOFactory'); 
		$objPHPExcel = new PHPExcel();
		$objPHPExcel->getProperties()->setTitle("用户账户信息")->setDescription(COMPANY_NAME); 
		$user_loan 				 = $this->Accountcash_model->arrays("user_loan");
		$account_bank 			 = $this->Accountcash_model->arrays("account_bank");
		$objPHPExcel->setActiveSheetIndex(0);
		$titles = array("编号", "用户名", "姓名", "类型", "银行", "省份", "城市", "开户行","提现帐号", "总额", "可用", "冻结", "代收");
		
		for($i = 0; $i < count($titles); $i ++){
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($i, 1, $titles[$i]);
		}
		$account_list = $vdata['account_list'];
		for($i = 0; $i < count($account_list); $i++){
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(0, $i+2, $account_list[$i]['user_id']);
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(1, $i+2, $account_list[$i]['username']);
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(2, $i+2, $account_list[$i]['realname']);
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(3, $i+2, $user_loan[$account_list[$i]['is_loan']]);
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(4, $i+2, @$account_bank[$account_list[$i]['bank']]);
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(5, $i+2, $account_list[$i]['province']?$area_list[$account_list[$i]['province']]['name']:'');
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(6, $i+2, $account_list[$i]['city']?$area_list[$account_list[$i]['city']]['name']:'');
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(7, $i+2, $account_list[$i]['branch']);
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(8, $i+2, $account_list[$i]['account'].' ');
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(9, $i+2, $account_list[$i]['total']);
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(10, $i+2, $account_list[$i]['use_money']);
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(11, $i+2, $account_list[$i]['no_use_money']);
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(12, $i+2, $account_list[$i]['collection']);
		}
		
		$objWriter = IOFactory::createWriter($objPHPExcel, 'Excel5'); 
		//发送标题强制用户下载文件 
		header('Content-Type: application/vnd.ms-excel'); 
		header('Content-Disposition: attachment;filename="用户账户信息.xls"'); 
		header('Cache-Control: max-age=0'); 
		$objWriter->save('php://output'); 
	}
	
	
	function accountstatlist(){
		$per_page 	= $this->input->get('per_page')?$this->input->get('per_page'):0;
		$username 	= $this->input->get('username');
		$is_loan 	= isset($_GET['is_loan']) ? $this->input->get('is_loan') : -1;
		$strtime	= $this->input->get('strtime');
		$endtime	= $this->input->get('endtime');
		
		$data = array();
		if($username != ''){
			$data['where']['username'] = $username;
		}
		if($is_loan != '' && $is_loan != -1){
			$data['where']['is_loan'] = $is_loan;
		}
		if($strtime != ''){
			$data['where']['strtime'] = $strtime;
		}
		if($endtime != ''){
			$data['where']['endtime'] = $endtime;
		}
		
		$data['page_size']  = YTZ_LIST_PAGE_SIZE;
		$data['page_start'] = (int)$per_page;
		$this->load->model('Account_model');
		$vdata = $this->Account_model->get_accountstat_list($data);
		
		//分页
		$this->load->library('pagination');
		$config['base_url'] = '/ychadmin/account/accountstatlist/?username='.$username.'&is_loan='.$is_loan.'&strtime='.$strtime.'&endtime='.$endtime;
		$config['total_rows'] = $vdata['account_count'];
		$config['per_page'] = YTZ_LIST_PAGE_SIZE; 
		$this->pagination->initialize($config); 
		
		$vdata['username'] 	= $username;
		$vdata['is_loan'] 	= $is_loan;
		$vdata['strtime'] 	= $strtime;
		$vdata['endtime'] 	= $endtime;
		$this->load->view('/ychadmin/accountstat_list', $vdata);
	}
	
	function accountstatexcel(){
		$username 	= $this->input->get('username');
		$is_loan 	= isset($_GET['is_loan']) ? $this->input->get('is_loan') : -1;
		$strtime	= $this->input->get('strtime');
		$endtime	= $this->input->get('endtime');
		
		$data = array();
		if($username != ''){
			$data['where']['username'] = $username;
		}
		if($is_loan != '' && $is_loan != -1){
			$data['where']['is_loan'] = $is_loan;
		}
		if($strtime != ''){
			$data['where']['strtime'] = $strtime;
		}
		if($endtime != ''){
			$data['where']['endtime'] = $endtime;
		}
		$data['limit'] = 'all';
		$data['page_size']  = YTZ_LIST_PAGE_SIZE;
		$data['page_start'] = 0;
		$this->load->model('Account_model');
		$vdata = $this->Account_model->get_accountstat_list($data);
		
		$this->load->library('PHPExcel');
		$this->load->library('PHPExcel/IOFactory'); 
		$objPHPExcel = new PHPExcel(); 
		$objPHPExcel->getProperties()->setTitle("用户资金统计")->setDescription(COMPANY_NAME); 
		
		
		$objPHPExcel->setActiveSheetIndex(0);
		$objPHPExcel->getActiveSheet()->SetCellValue('A1', 'Hello');
		$titles = array("编号", "用户名", "姓名", "类型", "线上充值", "线下充值", "平台充值", "提现", "到账", 
						"手续费", "扣除费用", "实际投资", "投标失败返回", "投标成功", "代收累计", "利息累计" );
		
		for($i = 0; $i < count($titles); $i ++){
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($i, 1, $titles[$i]);
		}
		$accountstat_list = $vdata['account_list'];
		for($i = 0; $i < count($accountstat_list); $i++){
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(0, $i+2, $accountstat_list[$i]['user_id']);
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(1, $i+2, $accountstat_list[$i]['username']);
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(2, $i+2, $accountstat_list[$i]['realname']);
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(3, $i+2, $accountstat_list[$i]['is_loan']);
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(4, $i+2, $accountstat_list[$i]['online_recharge']);
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(5, $i+2, $accountstat_list[$i]['local_recharge']);
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(6, $i+2, $accountstat_list[$i]['platform_recharge']);
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(7, $i+2, $accountstat_list[$i]['cash_money']);
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(8, $i+2, $accountstat_list[$i]['cash_success']);
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(9, $i+2, $accountstat_list[$i]['recharge_fee']);
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(10, $i+2, $accountstat_list[$i]['deduct_fee']);
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(11, $i+2, $accountstat_list[$i]['tender_money']);
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(12, $i+2, $accountstat_list[$i]['inverst_false']);
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(13, $i+2, $accountstat_list[$i]['inverst_success']);
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(14, $i+2, $accountstat_list[$i]['total_collection']);
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(15, $i+2, $accountstat_list[$i]['total_interest']);
		}
		
		$objWriter = IOFactory::createWriter($objPHPExcel, 'Excel5'); 
		//发送标题强制用户下载文件 
		header('Content-Type: application/vnd.ms-excel'); 
		header('Content-Disposition: attachment;filename="用户资金统计.xls"'); 
		header('Cache-Control: max-age=0'); 
		$objWriter->save('php://output'); 
	}
	
	function account_bank($user_id){
		$this->load->model('Account_bank_model', 'bank');
		$this->load->model('Area_model', 'area');
		$data['area'] = $this->area->get_list();
		$data['bank_list'] = $this->bank->arrays('var_account_bank');
		
		$bank = $this->bank->get_by_uid($user_id);
		$data['user_id'] = $user_id;
		$data['userinfo'] = $bank;
		
		$data['tab'] = $this->tab;
		$data['sub'] = 'account';
		$this->load->view('/ychadmin/accountbank', $data);
	}
	
	function edit_bank(){
		$this->load->model('Account_bank_model', 'bank');
		$this->load->model('User_model');
		$bank = $this->bank->get_by_uid($this->input->post('user_id'));
		if($bank){
			$setArr = array(
				'account' => $this->input->post('account'),
				'bank' => intval($this->input->post('bank')),
				'province' => intval($this->input->post('province')),
				'city' => intval($this->input->post('city')),
				'area' => intval($this->input->post('area')),
				'branch' => $this->input->post('branch'),
			); 
			$id = $this->bank->update($setArr,array('user_id'=>$this->input->post('user_id')));
			if($id>0){
				$this->load->model('log_model');$this->log_model->add_log('[修改银行账号][成功][ID=='.$this->input->post('user_id').']');
				$this->load->library('phone');
				$user_info = $this->User_model->get_all_by_id($this->input->post('user_id'));
				$this->phone->send($user_info['phone'],"您的提现账户已修改成功，如非本人操作，请及时联系客服！");
				echo '修改成功';
				exit;
			}else{
				echo '保存失败';
				exit;
			}
		}else{
			echo '该用户没有填下银行卡号';
			exit; 
		}
	}
	
	function jsinfo($cashid=0){
		$this->load->model('Accountcash_model');
		$this->load->model('Account_model');
		$cash = $this->Accountcash_model->get_by_id($cashid);
		$user_id = $cash['user_id'];
		$account = $this->Account_model->get_account($user_id);
		echo json_encode($account);
	}
}